﻿using System;
using Microsoft.VisualStudio.TestTools.UnitTesting;
using DropNet;
using System.IO;
using System.Configuration;

namespace CloudStorageLight.Core.Tests
{
    [TestClass]
    public class DropBoxBlobAdapterTest
    {
        public string appkey = ConfigurationManager.AppSettings["DropBoxAppKey"];
        public string appSecret = ConfigurationManager.AppSettings["DropBoxAppSecret"];
        public string accessToken = ConfigurationManager.AppSettings["DropBoxAccessToken"];
        public string accessSecret = ConfigurationManager.AppSettings["DropBoxAccessSecret"];

        [TestMethod]
        public void TestMethod1()
        {
            var client = new DropNetClient(appkey, appSecret, accessToken, accessSecret);
            using (var fs = new FileStream(@"C:\temp\result.txt", FileMode.Open))
            {
                var uploaded = client.UploadFile("/Public/", "result.txt", fs);
            }

        }

        [TestMethod]
        public void ConvertFtpPathToUrlPathTest()
        {
            using (var adp = new DropBoxBlobAdapter("/DropBox/", appkey, appSecret, accessToken, accessSecret, "/Public/", null))
            {
                var value = adp.ConvertDropPathToUrlPath("/Public/Sub1/A.txt");

                Assert.AreEqual("/DropBox/Sub1/A.txt", value);

            }
        }

        [TestMethod]
        public void ConvertUrlPathToFtpPathTest()
        {
            using (var adp = new DropBoxBlobAdapter("/DropBox/", appkey, appSecret, accessToken, accessSecret, "/Public/", null))
            {
                var value = adp.ConvertUrlPathToDropPath("/DropBox/Sub1/A.txt");

                Assert.AreEqual("/Public/Sub1/A.txt", value);

            }
        }

        [TestMethod]
        public void UploadAsyncAndDeleteAsyncCopyAsyncTest()
        {
            using (var adp = new DropBoxBlobAdapter("/DropBox/", appkey, appSecret, accessToken, accessSecret, "/Public/", null))
            {
                var temp = Path.GetTempFileName();
                try
                {
                    var itemA = new BlobFile(null, "/DropBox/A.txt");
                    var itemB = new BlobFile(null, "/DropBox/Sub1/Sub2/B.txt");
                    var itemBDire = new BlobDirectory(null, "/DropBox/Sub1/");
                    var itemB2Dire = new BlobDirectory(null, "/DropBox/Sub1/Sub2/");
                    adp.DeleteAsync(itemA).Wait();
                    adp.DeleteAsync(itemB).Wait();
                    adp.DeleteAsync(itemB2Dire).Wait();
                    adp.DeleteAsync(itemBDire).Wait();

                    var value = adp.ExistsAsync("/DropBox/A.txt").Result;
                    Assert.AreEqual(false, value);

                    adp.UploadAsync(itemA, temp).Wait();
                    value = adp.ExistsAsync("/DropBox/A.txt").Result;
                    Assert.AreEqual(true, value);

                    adp.CopyAsync("/DropBox/Sub1/Sub2/B.txt", itemA).Wait();
                    value = adp.ExistsAsync("/DropBox/Sub1/Sub2/B.txt").Result;
                    Assert.AreEqual(true, value);
                    var deleted = adp.DeleteAsync(itemB).Result;

                }
                finally
                {
                    File.Delete(temp);
                }

            }
        }

        [TestMethod]
        public void CreateDirectoryAsyncTest()
        {
            using (var adp = new DropBoxBlobAdapter("/DropBox/", appkey, appSecret, accessToken, accessSecret, "/Public/", null))
            {
                var temp = Path.GetTempFileName();
                try
                {
                    var itemDire = new BlobDirectory(null, "/DropBox/SubTemp/");
                    adp.DeleteAsync(itemDire).Wait();

                    var value = adp.ExistsAsync("/DropBox/SubTemp/").Result;
                    Assert.AreEqual(false, value);

                    adp.CreateDirectoryAsync(itemDire).Wait();
                    value = adp.ExistsAsync("/DropBox/SubTemp/").Result;
                    Assert.AreEqual(true, value);


                }
                finally
                {
                    File.Delete(temp);
                }

            }
        }

        [TestMethod]
        public void GetFilesAsyncTest()
        {
            using (var adp = new DropBoxBlobAdapter("/DropBox/", appkey, appSecret, accessToken, accessSecret, "/Public/",null))
            {
                var list = adp.GetFilesAsync("/DropBox/", 0, false).Result;
                Assert.IsTrue(list.Count > 0);

                var itemBDire = new BlobDirectory(null, "/DropBox/Sub1/Sub2/");
                adp.DeleteAsync(itemBDire).Wait();
                adp.CreateDirectoryAsync(itemBDire).Wait();
                var list2 = adp.GetFilesAsync("/DropBox/Sub1/", 0, true).Result;
                Assert.IsTrue(list2.Count > 0);
            }

        }
    }
}
